/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.WebServiceClient;

import controller.WSCTreatment.InsertTreatmentService;
import controller.WSCTreatment.SrvCureservice;
import controller.WSCTreatment.SrvCureserviceRecord;
import entity.MMapHospitalDoctor;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;
import session.MMapHospitalDoctorFacade;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="WSC_insertTreatment", 
            urlPatterns={"/WebServiceClient/WSC_insertTreatment",
            "/WebServiceClient/WSC_insertTreatment/formCureService_Submit"})
public class WSC_insertTreatment extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/suntiparb-pc_9484/insertTreatmentService/insertTreatmentPort.wsdl")
    private InsertTreatmentService service;
   
//    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/suntiparb-pc_9484/insertDoctorService/insertDoctorPort.wsdl")
//    private InsertDoctorService service;

    @EJB
    private MMapHospitalDoctorFacade mMapHospitalDoctorFacade;

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/WebServiceClient/WSC_insertDoctor")) {
            response.setContentType("text/html;charset=utf-8");

            userPath = "/WebServiceClient/WSC_insertDoctor";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();


        if (userPath.equals("/WebServiceClient/WSC_insertTreatment/formCureService_Submit")) {

            String txtName = (request.getParameter("txtName") != null) ? request.getParameter("txtName") : "";
            String txtPrice = (request.getParameter("txtPrice") != null) ? request.getParameter("txtPrice") : "";
            String txtCurrency = (request.getParameter("txtCurrency") != null) ? request.getParameter("txtCurrency") : "";
            String ddlDoctorId = (request.getParameter("ddlDoctorName") != null) ? request.getParameter("ddlDoctorName") : "";
            String ddlHospitalId = "1"; // fix for prototype

            List<MMapHospitalDoctor> lMMapHospitalDoctor;
            try {
                //Query Java Object
                lMMapHospitalDoctor = mMapHospitalDoctorFacade.findByDoctorId(ddlDoctorId);

                if (!lMMapHospitalDoctor.isEmpty()) {
                    for (MMapHospitalDoctor obj : lMMapHospitalDoctor) {
                        if (obj.getMDoctor() != null) {
                            ddlHospitalId = obj.getMHospital().getId().toString();
                        }
                    }
                }

            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            }

            PrintWriter out = response.getWriter();
            try {
                SrvCureserviceRecord listService = new SrvCureserviceRecord();
                SrvCureserviceRecord.Name tmpname = new SrvCureserviceRecord.Name();
                SrvCureserviceRecord.Price tmpprice = new SrvCureserviceRecord.Price();
                SrvCureserviceRecord.Currency tmpcurrency = new SrvCureserviceRecord.Currency();
                SrvCureserviceRecord.MDoctorID tmpdoctorId = new SrvCureserviceRecord.MDoctorID();
                SrvCureserviceRecord.MHospitalID tmphospitalId = new SrvCureserviceRecord.MHospitalID();
                tmpname.setValue(txtName);
                tmpprice.setValue(new BigDecimal(txtPrice));
                tmpcurrency.setValue(txtCurrency);
                tmpdoctorId.setValue(Long.parseLong(ddlDoctorId));
                tmphospitalId.setValue(Long.parseLong(ddlHospitalId));
                listService.setName(tmpname);
                listService.setPrice(tmpprice);
                listService.setCurrency(tmpcurrency);
                listService.setMDoctorID(tmpdoctorId);
                listService.setMHospitalID(tmphospitalId);
                SrvCureservice inputData = new SrvCureservice();
                inputData.getSrvCureserviceRecord().add(0, listService);
                String Message = insertTreatmentOperation(inputData);
                out.println(Message);
            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
            }

            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }

//    private String insertDoctorOperation(controller.WebServiceClient.MDoctor inputData) {
//        controller.WebServiceClient.InsertDoctorPortType port = service.getInsertDoctorPort();
//        return port.insertDoctorOperation(inputData);
//    }

    private String insertTreatmentOperation(controller.WSCTreatment.SrvCureservice inputData) {
        controller.WSCTreatment.InsertTreatmentPortType port = service.getInsertTreatmentPort();
        return port.insertTreatmentOperation(inputData);
    }
}
